import Vue from 'vue'
import Router from 'vue-router'
import '@/assets/css/global.css'
import Login from '@/components/Login'
import Home from '@/components/Home'
import Welcome from '@/components/Welcome'
import Thickcharts from '@/components/Thickcharts'
import Devices from '@/components/Devices'
import Corrosion from '@/components/Corrosion'



Vue.use(Router)

// export default new Router({
//     routes: [{
//             path: '/',
//             redirect: '/login'
//         },
//         {
//             path: '/login',
//             component: Login
//         },
//         {
//             path: '/home',
//             name: 'Home',
//             component: Home,
//             redirect: '/welcome',
//             children: [
//                 { path: '/welcome', component: Welcome },
//                 { path: '/thickcharts', component: Thickcharts },
//                 { path: '/devices', component: Devices },
//                 { path: '/corrosion', component: Corrosion },
//             ]
//         }

//     ]
// })
const routes = [{
        path: '/',
        redirect: '/login'
    },
    {
        path: '/login',
        component: Login
    },
    {
        path: '/home',
        name: 'Home',
        component: Home,
        redirect: '/welcome',
        children: [
            { path: '/welcome', component: Welcome },
            { path: '/thickcharts', component: Thickcharts },
            { path: '/devices', component: Devices },
            { path: '/corrosion', component: Corrosion },
        ]
    }
]

const router = new Router({
    routes
})

// 挂载路由卫士
router.beforeEach((to, from, next) => {
    if (to.path === '/login') return next()
    const tokenStr = window.sessionStorage.getItem('token')
    if (!tokenStr) {
        return next('/login')
    }
    next()

})
export default router